package com.yuanduo_app.bridge;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.xujl.fastlib.base.BaseModule;
import com.xujl.fastlib.utils.LogS;
import com.xujl.fastlib.utils.StringUtils;

import com.yuanduo_app.MainApplication;
import com.yuanduo_app.event.LoadingEvent;
import com.yuanduo_app.window.CommonInfoWindow;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.NonNull;

public class WindowModule extends BaseModule {
    private static final String TAG = "WindowModule";
    private static final String KEY_TITLE = "title";
    private static final String KEY_CONTENT = "content";
    private static final String KEY_LEFT_BTN_STR = "leftStr";
    private static final String KEY_LEFT_RIGHT_STR = "rightStr";
    private static final String KEY_ONLY_RIGHT_BTN = "onlyRightBtn";//仅显示一个右方按钮（居中显示）,1:是

    public WindowModule (@NonNull ReactApplicationContext reactContext) {
        super(reactContext);
        LogS.i(TAG, "create");
    }

    @NonNull
    @Override
    public String getName () {
        return "WindowModule";
    }

    @ReactMethod
    public void showWindow (ReadableMap params, Callback callback) {
        final CommonInfoWindow infoWindow = new CommonInfoWindow(MainApplication.getApplication(),
                CommonInfoWindow.createBuilder()
                        .setTitle(getValue(KEY_TITLE, params))
                        .setInfo(getValue(KEY_CONTENT, params))
                        .setRightStr(getValue(KEY_LEFT_RIGHT_STR, params))
                        .setLeftStr(getValue(KEY_LEFT_BTN_STR, params))
                        .setSingleBtn(StringUtils.equals(getValue(KEY_ONLY_RIGHT_BTN, params), "1"))
        );
        infoWindow.setCallback(code -> {
            if (callback == null) {
                return;
            }
            callback.invoke(code);
        });
        infoWindow.showPopupWindow();
    }


    @ReactMethod
    public void showLoading () {
        EventBus.getDefault().post(new LoadingEvent(true));
    }

    @ReactMethod
    public void dismissLoading () {
        EventBus.getDefault().post(new LoadingEvent(false));
    }


}
